package com.example.myapplication.tools;

import java.text.SimpleDateFormat;
import java.util.Date;

public class StringUtils {

    /**
     * 字符串转换为16进制字符串
     *
     * @param s
     * @return
     */
    public static String stringToHexString(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str;
    }

    /**
     * 16进制字符串转换为字符串
     *
     * @param s
     * @return
     */
    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                int temp = Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
                if(temp == 0 || temp == 255){
                    if(i > 0){
                        byte[] byteFinal = new byte[i];
                        System.arraycopy(baKeyword,0,byteFinal,0,i);
                        s = new String(byteFinal, "gbk");
                        return s;
                    }else{
                        return "";
                    }
                }else{
                    baKeyword[i] = (byte) (0xff & temp);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "gbk");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    /**
     * Obj转换String是，处理null、“null”。
     * @param obj
     * @return String
     */
    public static String transString(Object obj){
        String str ="";
        if(obj !=null){
            if(obj.getClass() == Date.class){
                try {
                    SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    str = sdf.format(obj);
                } catch (Exception e) {

                }
            }else{
                String tempStr = String.valueOf(obj);
                if(!tempStr.equals("null")){
                    str = tempStr;
                }
            }
        }
        return str;
    }

    public static String bytesToHex(byte[] src, String delimiter) {
        StringBuilder stringBuilder = new StringBuilder("");
        String strResult = "";
        if (src != null && src.length > 0) {
            for(int i = 0; i < src.length; ++i) {
                int v = src[i] & 255;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    stringBuilder.append(0);
                }

                stringBuilder.append(hv + delimiter);
            }

            String a = stringBuilder.toString();
            strResult = a.substring(0, stringBuilder.toString().length()).toUpperCase();
            return strResult;
        } else {
            return null;
        }
    }

    public static byte[] hexToByte(String hex) {
        int len = hex.length() / 2;
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();

        for(int i = 0; i < len; ++i) {
            int pos = i * 2;
            result[i] = (byte)(toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }

        return result;
    }

    private static byte toByte(char c) {
        byte b = (byte)"0123456789ABCDEF".indexOf(c);
        return b;
    }

}
